/**
 * 
 */
package bai9.com.server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

/**
 * @author nglehien Oct 22, 2012 - 10:06:58 PM
 */
public class StringHandleServer extends UnicastRemoteObject implements
		StringHandleInterface {

	private static final long serialVersionUID = 8734414276322028889L;
	public static final String STRING_HANDLE = "STRING_HANDLE";
	private String input = "Cong hoa xa hoi chu nghia viet nam";

	/**
	 * @throws RemoteException
	 */
	protected StringHandleServer() throws RemoteException {
		super();
	}

	@Override
	public String toUpper() throws RemoteException {
		return input.toUpperCase();
	}

	@Override
	public String toLower() throws RemoteException {
		// TODO Auto-generated method stub
		return input.toLowerCase();
	}

	@Override
	public boolean insert(int position, String str) throws RemoteException {
		try {
			StringBuilder builder = new StringBuilder(input);
			builder = builder.insert(position, str);
			this.input = builder.toString();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public boolean delete(String delete) throws RemoteException {
		try {
			StringBuilder builder = new StringBuilder(input);
			int index = input.indexOf(delete);
			int lengt = delete.length();
			builder = builder.delete(index, index + lengt);
			this.input = builder.toString();
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public void init(String input) throws RemoteException {
		this.input = input;
	}

	@Override
	public String getString() throws RemoteException {
		// TODO Auto-generated method stub
		return this.input;
	}

	@Override
	public boolean insertAfter(String position, String str)
			throws RemoteException {
		int index = input.indexOf(position);
		insert(index + position.length() + 1, str + " ");
		return false;
	}

	@Override
	public boolean insertBefore(String position, String str)
			throws RemoteException {
		int index = input.indexOf(position);
		insert(index, str + " ");
		return false;
	}

}
